/*************************************************************************
 *************************************************************************
 	 	 	 	 	 	 	M2SMA : SDLTestAgent.cpp
 *************************************************************************
 *
 	 Copyright (C) 2011
 	 TODO, Steven Costiou, France
 *
 **************************************************************************
 **************************************************************************
 *
	TODO FILE COMMENT
 *
 **************************************************************************
 **************************************************************************
 *
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/
 *
 **************************************************************************
 **************************************************************************/

#include "SDLTestAgent.h"


SDLTestAgent::SDLTestAgent(Engine* parentEngine) : Agent(parentEngine)
{
	addClassName("SDL_AGENT");

	agentStarted = false;

	//Les attributs de l'�cran (640 * 480)
	SCREEN_WIDTH = 640;
	SCREEN_HEIGHT = 480;
	SCREEN_BPP = 32;

		//Les attributs de la feuille de sprites
	SHEET_WIDTH = 200;
	SHEET_HEIGHT = 200;

		//Les surfaces
	faces = NULL;
	screen = NULL;
}

SDLTestAgent::~SDLTestAgent()
{

}

SDL_Surface* SDLTestAgent::load_image( std::string filename )
{
    //L'image qui est charg�e
    SDL_Surface* loadedImage = NULL;

    //L'image optimis�e qu'on va utiliser
    SDL_Surface* optimizedImage = NULL;

    //Chargement de l'image
    loadedImage = IMG_Load( filename.c_str() );

    //Si l'image est charg�e
    if( loadedImage != NULL )
    {
        //Cr�ation de l'image optimis�e
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Lib�ration de l'ancienne image
        SDL_FreeSurface( loadedImage );

        //Si la cr�ation de l'image optimis�e s'est bien pass�e
        if( optimizedImage != NULL )
        {
            SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }

    //On retourne l'image optimis�e
    return optimizedImage;
}

void SDLTestAgent::apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;

    //On blitte la surface
    SDL_BlitSurface( source, clip, destination, &offset );
}

bool SDLTestAgent::init()
{
    //Initialisation de tous les sous-syst�mes de SDL
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Mise en place de l'�cran
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //S'il y a une erreur lors de la mise en place de l'�cran
    if( screen == NULL )
    {
        return false;
    }

    //Mise en place de la barre caption
    SDL_WM_SetCaption( "Split the faces", NULL );

    //Si tout s'est bien pass�
    return true;
}

bool SDLTestAgent::load_files()
{
    //Chargement de la feuille de sprite
    faces = load_image( "/home/rstv/workspace/Darth-Wouf-2011/ressources/faces.png" );

    //S'il y a eu un probl�me au chargement de la feuille de sprites
    if( faces == NULL )
    {
        return false;
    }

    //Si tout s'est bien pass�
    return true;
}

void SDLTestAgent::clean_up()
{
    //On lib�re la feuille de sprites
    SDL_FreeSurface( faces );

    //On quitte SDL
    SDL_Quit();

    this->stopSystem();
}

void SDLTestAgent::startAgent()
{
	//Initialisation
		   if( init() == false )
		   {
		       cout << "Init = false" << endl;
		   }

		    //Chargement des fichiers
		    if( load_files() == false )
		    {
		    	cout << "load files = false" << endl;
		    }

		    //On coupe la partie en haut � gauche (premier sprite)
	    clip[ 0 ].x = 0;
	    clip[ 0 ].y = 0;
	    clip[ 0 ].w = SHEET_WIDTH/2;
	    clip[ 0 ].h = SHEET_HEIGHT/2;

	    //On coupe la partie en haut � droite (second sprite)
	    clip[ 1 ].x = SHEET_WIDTH/2;
	    clip[ 1 ].y = 0;
	    clip[ 1 ].w = SHEET_WIDTH/2;
	    clip[ 1 ].h = SHEET_HEIGHT/2;

	    //On coupe la partie en bas � gauche (troisi�me sprite)
	    clip[ 2 ].x = 0;
	    clip[ 2 ].y = SHEET_HEIGHT/2;
	    clip[ 2 ].w = SHEET_WIDTH/2;
	    clip[ 2 ].h = SHEET_HEIGHT/2;

	    //On coupe la partie en bas � droite (quatri�me sprite)
	    clip[ 3 ].x = SHEET_WIDTH/2;
	    clip[ 3 ].y = SHEET_HEIGHT/2;
	    clip[ 3 ].w = SHEET_WIDTH/2;
	    clip[ 3 ].h = SHEET_HEIGHT/2;


	    //On remplit l'�cran de blanc
	    SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 255, 255, 255 ) );

	    //On applique les sprites sur l'�cran
	    apply_surface( 0, 0, faces, screen, &clip[ 0 ] );
	    apply_surface( SCREEN_WIDTH-(SHEET_WIDTH/2), 0, faces, screen, &clip[ 1 ] );
	    apply_surface( 0, SCREEN_HEIGHT-(SHEET_HEIGHT/2), faces, screen, &clip[ 2 ] );
	    apply_surface( SCREEN_WIDTH-(SHEET_WIDTH/2), SCREEN_HEIGHT-(SHEET_HEIGHT/2), faces, screen, &clip[ 3 ] );

}

void SDLTestAgent::live()
{
	if(!agentStarted)
		startAgent();

	cout << "This is SDL test agent : " << this->getName() << endl;

    // centre the bitmap on screen
    SDL_Rect dstrect;
    dstrect.x = (screen->w - SHEET_WIDTH) / 2;
    dstrect.y = (screen->h - SHEET_HEIGHT) / 2;
    //Tant que l'utilisateur n'a pas quitter


        //Tant qu'il y a un �v�nement
        while( SDL_PollEvent( &event ) )
        {
            //Si l'utilisateur � appuyer sur le X de la fen�tre
            if( event.type == SDL_QUIT )
            {
            	clean_up();
            }
        }
     SDL_Flip(screen);



}
